package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.task.ArticleHtmlTasks;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-06-30
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {
    @Autowired
    private IApArticleContentService apArticleContentService;
    @Autowired
    private ArticleHtmlTasks articleHtmlTasks;

    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        if (dto.getId() != null) {
            //修改
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            //为了保险起见,apArticle中的id设置为null
            this.updateById(apArticle);
            LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper();
            updateWrapper.eq(ApArticleContent::getArticleId, apArticle.getId());//对应的是where后面的内容
            updateWrapper.set(ApArticleContent::getContent,dto.getContent()); //相当于set后面的内容
            apArticleContentService.update(updateWrapper);
            return ResponseResult.okResult(apArticle.getId());
        }else{
            //新增的操作,需要保存两张表的数据
            ApArticle apArticle =new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
            //为了保险起见 ID设置为null
            apArticle.setId(null);
            this.save(apArticle);
            ApArticleContent apArticleContent =new ApArticleContent();
            apArticleContent.setId(null);
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            articleHtmlTasks.createHtml(apArticle,dto.getContent());
            return ResponseResult.okResult(apArticle.getId());

        }
    }

    @Override
    public ResponseResult load(ArticleHomeDto dto, int i) {

        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
//        查询条件：频道id 如果是0 就是查询所有频道
        if (dto.getChannelId()!=0) {
            queryWrapper.eq(ApArticle::getChannelId,dto.getChannelId());
        }

        if(i==0){
            //        0 代表初次加载或加载更新  发布时间>maxTime
            queryWrapper.gt(ApArticle::getPublishTime,dto.getMaxTime());
        }else{
            //      1代表加载更多  发布时间<minTime
            queryWrapper.lt(ApArticle::getPublishTime,dto.getMinTime());
        }
        //         隐藏了两个条件： 是否下架 是否删除
        queryWrapper.eq(ApArticle::getIsDelete,false);
        queryWrapper.eq(ApArticle::getIsDown,false);
        //        发布时间排序
        queryWrapper.orderByDesc(ApArticle::getPublishTime)  ;

//        分页
        Page<ApArticle> page = new Page<>(1,dto.getSize());

        Page<ApArticle> page1 = this.page(page, queryWrapper);



        return ResponseResult.okResult(page1.getRecords());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        // 根据id查询文章
        ApArticle apArticle = this.getById(dto.getArticleId());
        if (apArticle == null || apArticle.getIsDelete() || apArticle.getIsDown()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 根据id查询文章内容
        LambdaQueryWrapper<ApArticleContent> query = new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent articleContent = apArticleContentService.getOne(query);
        // 按照前端的格式拼接map
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", articleContent);
        // 返回通用的响应
        return ResponseResult.okResult(map);
    }
}
